// pages/share/index.js
var app   = getApp()
Page({
  data:{
    array: [{key:'',value:'全部'}, {key:'Beijing',value:'北京'}, {key:'Shanghai', value:'上海'}, {key:'Xian',value:'西安'}, {key:'Sanya',value:'三亚'}, {key:'Chengdu',value:'成都'}],
    index: 0
  },
  bindPickerChange: function(e) {
    console.log(this.data.array[e.detail.value].key)
    this.setData({
      index: e.detail.value
    })
  },
  bindStartDateChange: function(e) {
    this.setData({
      sdate: e.detail.value
    })
  },
  bindEndDateChange: function(e) {
    this.setData({
      edate: e.detail.value
    })
  },
  formSubmit: function(e) {
    var that = this
    console.log(e.detail.value)
    var sdate = e.detail.value.sdate
    var edate = e.detail.value.edate
    console.log(sdate)
    app.getJson('wxapp/search',{'area':this.data.array[e.detail.value.area].key,'sdate':sdate,'edate':edate},function(res){
      console.log(res)
      that.setData({
        caravanarray:res.data
      })      
    })

    // var user = wx.getStorageSync('$user')
    // app.getJson('wxapp/user/notice', {'touser':user.openId,'formId':e.detail.formId},function(){
    //   wx.showToast({
    //     title: '成功',
    //     icon: 'success',
    //     duration: 2000
    //   })}
    // )
  },
  formReset: function() {
    console.log('form发生了reset事件')
  },
  onLoad:function(options){
    var that = this
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var sdate = [year, month, day].map(function(n){
      n = n.toString()
      return n[1] ? n : '0' + n
    }).join('-')

    that.setData({
      sdate : sdate,
      edate : sdate
    })
    
    // 页面初始化 options为页面跳转所带来的参数    
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  search: function() {
    console.log('查询分享')
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 10000
    })
    if (wx.getStorageSync('$share_list')) {
        wx.navigateTo({
          url: 'list?page=1'
        })
        wx.hideToast()
    }else{
      app.getJson('test/test', {'page':1}, function(res){
        if (res.data.data) {
          wx.setStorageSync('$share_list', res.data.data)
          wx.navigateTo({
            url: 'list?page=1'
          })
          wx.hideToast()
        }
      })
    }
  }
})